﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Bansud.SGR.BizSGRSecurity;
using condortravel.operaciones.core;

namespace condortravel.operaciones.persistence.Security
{
    public class SecurityDAO
    {
        #region Attributes
        private ImpersonationBD.ImpersonationBD impersonationBD;
        private AppSeguridad appSeguridad;
        #endregion

        #region Properties

        public AppSeguridad AppSeguridad
        {
            set {
                appSeguridad = value;
            }
        }

        public bool IsImpersonate
        {
            get
            {
                return impersonationBD.IsImpersonate;
            }

        }

        public Impersonation Impersonation
        {
            get
            {
                if (!impersonationBD.IsImpersonate)
                    throw new Exception("It does  not have impersonation");
                return impersonationBD.Impersonation;
            }

        }
        public ImpersonationBD.ImpersonationBD ImpersonationBD
        {
            set
            {
                impersonationBD = value;
            }

        }
        #endregion

        public bool IsAuthenticate(string appCode,string windowsID)
        {
            using (ImpersonationHelper imp = new ImpersonationHelper(this.Impersonation.User, this.Impersonation.Domain, this.Impersonation.Password))
            {
                if (IsImpersonate)
                {
                    if (!imp.ImpersonateUser())
                    {
                        throw new Exception("The Impersonate has failed");
                    }

                }
                appSeguridad.CodAplicativo = appCode;
                appSeguridad.Autenticacion(windowsID, string.Empty, string.Empty);
                return appSeguridad.Autenticado;
            }

        }

        public InfoDatosUsuario getInfoUser()
        {
            return appSeguridad.InfoDatosUsuario;
        }
        public DataTable getOptions(string type, string culture)
        {
            using (ImpersonationHelper imp = new ImpersonationHelper(this.Impersonation.User, this.Impersonation.Domain, this.Impersonation.Password))
            {
                if (IsImpersonate)
                {
                    if (!imp.ImpersonateUser())
                    {
                        throw new Exception("The Impersonate has failed");
                    }

                }
                return appSeguridad.ObtenerOpciones("OM", culture);
            }
        }

    }
}
